# MakeBy: LiuBoyuan
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView

from framework.SerableQueen import SerableQueen


class Join(APIView):

    def post(self, request: Request):
        """
        注册用户数据
        :param request:
        :return:
        """

        serableQueue = SerableQueen()
        userManager = serableQueue.get_UserManger()
        datas = request.data
        random_token = userManager.random_token_generate()
        userpower = datas.get("power")
        assert userpower is not None

        userManager.join_user(random_token, userpower)
        return Response({
            'token': random_token
        })

    def get(self, request: Request):
        """
        获取用户信息
        :param request:
        :return:
        """
        token = request.query_params.get("token")
        assert token is not None
        serableQueue = SerableQueen()
        userManager = serableQueue.get_UserManger()
        ret = userManager.validate(token)
        is_validate = ret[0]

        if is_validate is False:
            return Response("none")
        else:
            # TODO 加载用户数据
            ret = ret[1]
            return Response(ret)